# Direct outputs eligible for public release

# GLOBAL SETTINGS --------------------------------------------------------------

options(
    scipen = 999,
    digits = 16,
    max.print = .Machine$integer.max,
    show.error.locations = TRUE,
    warn = 1
)

RNGkind("L'Ecuyer-CMRG")
seed <- 818675309L
set.seed(seed) # setting main seed

# PACKAGES ---------------------------------------------------------------------
library(data.table)
library(ggplot2)
library(scales)

# PACKAGE SETTINGS -------------------------------------------------------------

# data.table
setDTthreads(threads = 1L)
options(datatable.print.class = TRUE, datatable.print.keys = TRUE)
# so that printing the data.table also shows the variable type on top

# BEGIN FILE -------------------------------------------------------------------

# READ IN OUR DATA -------------------------------------------------------------
# Focus on the age-group-specific estimates across time aggregations

per_adult_total_empl_income_dt <-
    readRDS(
        "~/estimation-output/unearned_income_effect_estimates_per_adult_total_empl_income_by_age.rds"  #nolint
    )
setnames(per_adult_total_empl_income_dt, "ref_event_time", "event_time")

implied_consumption_dt <-
    readRDS(
        "~/estimation-output/unearned_income_effect_estimates_implied_consumption_by_age.rds"  #nolint
    )
setnames(implied_consumption_dt, "ref_event_time", "event_time")

# Produce graph labels
per_adult_total_empl_income_dt[
    ,
    age_group :=
    factor(
        age_group,
        levels = c("Younger Winners", "Older Winners")
    )
]

per_adult_total_empl_income_dt[, time_window_label := as.character(NA)]
per_adult_total_empl_income_dt[
    event_time == 99, time_window_label := "Avg (+1 to +5)"
]
per_adult_total_empl_income_dt[
    event_time == 97, time_window_label := "Short Run (+1 to +2)"
]
per_adult_total_empl_income_dt[
    event_time == 98, time_window_label := "Long Run (+3 to +5)"
]
per_adult_total_empl_income_dt[
    ,
    time_window_label :=
        factor(
            time_window_label,
            levels = c("Avg (+1 to +5)", "Short Run (+1 to +2)", "Long Run (+3 to +5)") #nolint
        )
]

implied_consumption_dt[
    ,
    age_group :=
    factor(
        age_group,
        levels = c("Younger Winners", "Older Winners")
    )
]

implied_consumption_dt[, time_window_label := as.character(NA)]
implied_consumption_dt[
    event_time == 99, time_window_label := "Avg (+1 to +5)"
]
implied_consumption_dt[
    event_time == 97, time_window_label := "Short Run (+1 to +2)"
]
implied_consumption_dt[
    event_time == 98, time_window_label := "Long Run (+3 to +5)"
]
implied_consumption_dt[
    ,
    time_window_label :=
        factor(
            time_window_label,
            levels = c("Avg (+1 to +5)", "Short Run (+1 to +2)", "Long Run (+3 to +5)") #nolint
        )
]

# Just to make it readable against to first, dark bar
errorbar_palette <-
    c(
        "#999999",
        "#000000"
    )

# Figure 4.2a -- unearned income effects across time and by age group
# per-adult total labor earnings

figure_4_2_a <-
    ggplot(
        data = per_adult_total_empl_income_dt,
        aes(
            x = factor(time_window_label),
            y = estimate,
            fill = factor(age_group)
        )
    ) +
    geom_bar(stat = "identity", position = position_dodge()) +
    geom_errorbar(aes(
        ymin = ((estimate - (1.64 * cluster_se))),
        ymax = ((estimate + (1.64 * cluster_se))),
        color = factor(age_group)
    ),
    position = position_dodge(0.9),
    show.legend = FALSE,
    width = 0.2
    ) +
    theme_bw(base_size = 11.5) +
    theme(
        panel.grid.minor = element_blank(), # remove actual gridlines
        panel.grid.major = element_blank()
    ) +
    scale_y_continuous(breaks = (seq.int(from = -100, to = 0, by = 10) / 100)) +
    theme(
        legend.position = "bottom",
        legend.title = element_blank(),
        legend.key.size = unit(1.75, "lines")
    ) +
    labs(x = "Timing", y = "Effect per $1 of Unearned Income") +
    geom_vline(xintercept = 1.5, linetype = 3) +
    scale_fill_viridis_d() +
    scale_color_manual(values = errorbar_palette) +
    coord_cartesian(ylim = c(-1, 0))

ggsave(
    plot = figure_4_2_a,
    filename = "~/paper/figures/figure-4.2a.png",
    width = 6,
    height = 4,
    dpi = 600
)

ggsave(
    plot = figure_4_2_a,
    filename = "~/paper/figures/figure-4.2a.tif",
    width = 6,
    height = 4,
    device = "tiff",
    dpi = 600
)

# Figure 4.2b -- unearned income effects across time and by age group
# implied consumption expenditure

figure_4_2_b <-
    ggplot(
        data = implied_consumption_dt,
        aes(
            x = factor(time_window_label),
            y = estimate,
            fill = factor(age_group)
        )
    ) +
    geom_bar(stat = "identity", position = position_dodge()) +
    geom_errorbar(aes(
        ymin = ((estimate - (1.64 * cluster_se))),
        ymax = ((estimate + (1.64 * cluster_se))),
        color = factor(age_group)
    ),
    position = position_dodge(0.9),
    show.legend = FALSE,
    width = 0.2
    ) +
    theme_bw(base_size = 11.5) +
    theme(
        panel.grid.minor = element_blank(), # remove actual gridlines
        panel.grid.major = element_blank()
    ) +
    scale_y_continuous(breaks = (seq.int(from = 0, to = 100, by = 10) / 100)) +
    theme(
        legend.position = "bottom",
        legend.title = element_blank(),
        legend.key.size = unit(1.75, "lines")
    ) +
    labs(x = "Timing", y = "Effect per $1 of Unearned Income") +
    geom_vline(xintercept = 1.5, linetype = 3) +
    scale_fill_viridis_d() +
    scale_color_manual(values = errorbar_palette) +
    coord_cartesian(ylim = c(0, 1))

ggsave(
    plot = figure_4_2_b,
    filename = "~/paper/figures/figure-4.2b.png",
    width = 6,
    height = 4,
    dpi = 600
)

ggsave(
    plot = figure_4_2_b,
    filename = "~/paper/figures/figure-4.2b.tif",
    width = 6,
    height = 4,
    device = "tiff",
    dpi = 600
)

# Housekeeping
per_adult_total_empl_income_dt <- NULL
implied_consumption_dt <- NULL
errorbar_palette <- NULL
figure_4_2_a <- NULL
figure_4_2_b <- NULL
rm(
    per_adult_total_empl_income_dt,
    implied_consumption_dt,
     errorbar_palette,
     figure_4_2_a,
     figure_4_2_b
)